package com.frame.flux.ui;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.data.entity.Image;
import com.ui.main.R;
import com.util.ImageUtil;

import java.util.ArrayList;

/**
 * @Description
 * @Author luxiao418
 * @Email luxiao418@pingan.com.cn
 * @Date 16/8/13 01:06
 * @Version v2.6.0
 */
public class SimpleRecyclerCardAdapter extends RecyclerView.Adapter<SimpleCardViewHolder> {

    private LayoutInflater mInflater;
    private OnItemActionListener mOnItemActionListener;
    private ArrayList<Image> mResults;

    public SimpleRecyclerCardAdapter(Context mCtx, ArrayList<Image> results) {
        super();
        mResults = results;
        mInflater = LayoutInflater.from(mCtx);
    }

    @Override
    public int getItemCount() {
        return mResults.size();
    }

    @Override
    public void onBindViewHolder(final SimpleCardViewHolder viewHolder, int position) {
        viewHolder.itemTv.setText(mResults.get(position).title);
        ImageUtil.loadImg(viewHolder.itemIv, mResults.get(position).image);
        if (mOnItemActionListener != null) {
            //注意这里必须使用viewHolder.getPosition()而不能用i，因为为了保证动画，没有使用NotifyDatasetChanged更新位置数据
            viewHolder.itemView.setOnClickListener(v -> mOnItemActionListener.onItemClickListener(v, viewHolder.getPosition()));

            //注意这里必须使用viewHolder.getPosition()而不能用i，因为为了保证动画，没有使用NotifyDatasetChanged更新位置数据
            viewHolder.itemView.setOnLongClickListener(v -> mOnItemActionListener.onItemLongClickListener(v, viewHolder.getPosition()));
        }
    }

    @Override
    public SimpleCardViewHolder onCreateViewHolder(ViewGroup viewgroup, int i) {

        View v = mInflater.inflate(R.layout.simple_card_item, viewgroup, false);
        SimpleCardViewHolder simpleViewHolder = new SimpleCardViewHolder(v);
        simpleViewHolder.setIsRecyclable(true);

        return simpleViewHolder;
    }

    /**********
     * 定义点击事件
     **********/
    public interface OnItemActionListener {
        public void onItemClickListener(View v, int pos);
        public boolean onItemLongClickListener(View v, int pos);
    }

    public void setOnItemActionListener(OnItemActionListener onItemActionListener) {
        this.mOnItemActionListener = onItemActionListener;
    }
}

class SimpleCardViewHolder extends RecyclerView.ViewHolder {
    public TextView itemTv;
    public ImageView itemIv;

    public SimpleCardViewHolder(View layout) {
        super(layout);
        itemTv = (TextView) layout.findViewById(R.id.item_title);
        itemIv = (ImageView) layout.findViewById(R.id.item_img);
    }
}
